import {MyMap} from "../../../utils/lodash";
import {getLocal} from "../../../utils/local";

export async function get_fabric() {
    var query = new AV.Query('fabric');
    var a = await query.find().then(result => result.map(item => item.toJSON())).catch(e => {
        console.error(e)
    });
    return a

}

export async function add_to_shop_cart({quantity, fabric, design}) {
    /*生成 shop_cart_item*/
    /*查看user 的shop_cart  or create 的 shop_cart */
    /*relative shop_cart_item and shop_cart*/
    var loginUser = getLocal("app.login_user")

    //2
    var shopcartQuery = new AV.Query('shopcart');
    var userWithoutData = AV.Object.createWithoutData('user', loginUser.objectId);
    var designWithoutData = AV.Object.createWithoutData('design', design.objectId);
    shopcartQuery.equalTo('user', userWithoutData);
    var shopcartQueryfindResult = await shopcartQuery.find().catch(e => console.error(e))

    var shopcartData = {
        user: userWithoutData,
    }
    var shopcart = new AV.Object('shopcart');
    if (shopcartQueryfindResult.length == 0) {
        //    create shop cart
        MyMap(shopcartData, (value, key) => {
            shopcart.set(key, value)
        })
        await shopcart.save().catch(e => {
            throw {message: e}
        });
    } else {
        shopcart.id = shopcartQueryfindResult[0].getObjectId()
        MyMap(shopcartData, (value, key) => {
            shopcart.set(key, value)
        })
        await shopcart.save().catch(e => {
            throw {message: e}
        });
    }

    //1
    var ShopcartItem = AV.Object.extend('shopcartItem');
    var shopcartitem = new ShopcartItem();
    var shopcartitemData = {
        totalPrice: fabric.unitPrice * quantity,
        quantity,//长度
        fabric: AV.Object.createWithoutData('fabric', fabric.objectId),
        unitPrice: fabric.unitPrice,
        design: designWithoutData,
        shopcart
    }
    MyMap(shopcartitemData, (value, key) => {
        shopcartitem.set(key, value)
    })
    var add_shopcartitem_result = await shopcartitem.save().catch(function (error) {
        console.error(error);
    });

}

export async function get_shop_cart_items() {
    var loginUser = getLocal("app.login_user")
    var query_shopcart = new AV.Query('shopcart');
    var userWithoutData = AV.Object.createWithoutData('user', loginUser.objectId);
    query_shopcart.equalTo('user', userWithoutData);
    var query_shopcart_result = await  query_shopcart.find()
    if (query_shopcart_result.length == 0) {
        throw {message: "没有购物车"}
    }
    var shopcart = query_shopcart_result[0]
    var query_shopcartitem = new AV.Query('shopcartItem');
    query_shopcartitem.equalTo('shopcart', shopcart);
    query_shopcartitem.include("fabric")
    query_shopcartitem.include("design")
    var shopcartItems = await  query_shopcartitem.find().then(result => result.map(item => item.toJSON()))
    return shopcartItems
}

export function init_fabric() {
    // var fabricWithoutData = AV.Object.createWithoutData('fabric')
    var objectsData = [
        {
            name: "标准棉布",
            weight: "",
            width: "1.32",
            unitPrice: "80"
        },
        {
            name: "弹性棉布",
            weight: "",
            width: "1.32",
            unitPrice: "86"
        }, {
            name: "帆布棉",
            weight: "",
            width: "1.37",
            unitPrice: "96"
        }, {
            name: "运动莱卡",
            weight: "",
            width: "1.42",
            unitPrice: "90"
        }, {
            name: "色丁布",
            weight: "",
            width: "1.34",
            unitPrice: "86"
        }, {
            name: "化纤布",
            weight: "",
            width: "1.42",
            unitPrice: "90"
        }
    ]; // 构建一个本地的 AV.Object 对象数组
    var objects = [];
    MyMap(objectsData, item => {
        var fabricObJECT = new (AV.Object.extend('fabric'))();
        MyMap(item, (value, key) => {
            fabricObJECT.set(key, value)
        })
        objects.push(fabricObJECT)
    })


    // 批量创建（更新）
    AV.Object.saveAll(objects).then(function (objects) {
        console.log(objects)
        // 成功
    }, function (error) {
        // 异常处理
        console.error(objects)
    });
}